﻿#region Header

/*
Behavioral Rating of Dancing Human Crowds based on Motion Patterns
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using Emgu.CV;
using Emgu.CV.Structure;
using Sebarf.Services.Interfaces;

#endregion

namespace paravili.Services {
	/// <summary>
	/// Description of the class
	/// </summary>
	public class ImageProviderLocatorService : Service, IImageLocatorService, IImageProviderService {
		#region Public Properties

		[ServiceRequest]
		public IDispatcherLocatorService DispatcherLocatorService { get; set; }

		[ServiceRequest]
		public IResetHandlerService ResetHandlerService {
			get { return null; }
			set { value.Reset += value_Reset; }
		}

		#endregion

		#region Public Methods

		public ImageProviderLocatorService() {
			Images = new ObservableCollection<ImageEntry>();
		}

		#endregion

		#region Private Methods

		private void value_Reset(object sender, EventArgs e) {
			Images.Clear();
			m_entryById.Clear();
		}

		#endregion

		#region Private Fields

		private readonly Dictionary<string, ImageEntry> m_entryById = new Dictionary<string, ImageEntry>();

		#endregion

		#region IImageLocatorService Members

		public Image<Lab, byte> GetImage(string id) {
			if (!m_entryById.ContainsKey(id)) {
				return null;
			}
			return m_entryById[id].ImageData;
		}

		public ObservableCollection<ImageEntry> Images { get; set; }

		#endregion

		#region IImageProviderService Members

		public void SetImage(string id, Image<Lab, byte> image) {
			DispatcherLocatorService.GetDispatcher().Invoke(new ThreadStart(delegate {
				if (m_entryById.ContainsKey(id)) {
					m_entryById[id].ImageData = image;
				}
				else {
					var entryToAdd = new ImageEntry { Id = id, ImageData = image };
					m_entryById.Add(id, entryToAdd);
					Images.Add(entryToAdd);
				}
			}), null);
		}

		#endregion

		public void UpdateImage(string id) {
			m_entryById[id].ImageData = m_entryById[id].ImageData;
		}
	}
}